//
//  KCLoginViewController.swift
//  MyKC
//
//  Created by yy on 2017/1/18.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit
import SVProgressHUD

class KCLoginViewController: UIViewController {
    @IBOutlet weak var btn_remberPassword: UIButton!
    @IBOutlet weak var pwdView: UIView!
    @IBOutlet weak var tf_userName: UITextField!
    @IBOutlet weak var tf_password: UITextField!
    @IBAction func tfValueChanged(_ sender: UITextField) {
        btn_login.isEnabled = tf_password.hasText && tf_userName.hasText
    }
    @IBAction func btnHistoryUserClick(_ sender: UIButton) {
        //获取所有的数据加载到tableView上面
    }
    // MARK:  是否记住密码按钮点击事件-----
    @IBAction func remberPwdBtnClick(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        shareAppSetting.keepLogin = !shareAppSetting.keepLogin
        if !shareAppSetting.keepLogin {
            tf_password.text = ""
        }
    }
    @IBOutlet weak var btn_login: UIButton!
    // MARK: - 登录按钮点击事件
    @IBAction func loginBtnClick(_ sender: UIButton) {
        SVProgressHUD.show()
        let dic = Bundle.main.infoDictionary! as NSDictionary
        let appVerssion = dic.value(forKey: "CFBundleShortVersionString") as! String
        let para = ["Account": tf_userName.text ?? "", "Password": tf_password.text ?? "","LoginFromType": "IOS","AppVersion":appVerssion]
        KCNetworkManager.defaultTools.loginRequest(para: para) {[weak self] (response, error) in
            SVProgressHUD.dismiss()
            guard let response = response,
                let responseObj = response as? NSDictionary,
                let responseContent = responseObj["Content"] as? NSDictionary,
                let rowType = responseContent["RoleType"] as? NSString,
                let accesstoken = responseContent["AccessToken"] as? String else {
                return
            }
            if(rowType.integerValue == 5000420 || rowType.integerValue == 5000430) {
                //是督导
                shareAppSetting.userType = rowType as String
                self?.jumpToVC(identifier: "MainDuDaoSB", accessToken: accesstoken)
            }else if(rowType.integerValue == 5000410 || rowType.integerValue == 5000450) {
                //是促销员
                shareAppSetting.userType = rowType as String
                self?.jumpToVC(identifier: "Main", accessToken: accesstoken)
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
}


extension KCLoginViewController {
    // MARK:  设置ui界面---------
    func setupUI() {
        tf_userName.text = shareAppSetting.userName
        if shareAppSetting.keepLogin {
            btn_remberPassword.isSelected = true
            tf_password.text = shareAppSetting.password
        }
        btn_login.isEnabled = tf_password.hasText && tf_userName.hasText
    }
    
    // MARK:  登录成功  保存token和账户密码   切换根控制器
    func jumpToVC(identifier: String,accessToken: String) -> Void {
        //保存数据
        shareAppSetting.accessToken = accessToken
        shareAppSetting.userName = tf_userName.text
        shareAppSetting.password = tf_password.text
        let sb = UIStoryboard(name:identifier, bundle: nil)
        let tabVC = sb.instantiateInitialViewController()
        UIApplication.shared.keyWindow?.rootViewController = tabVC
    }
}


